package cpr.command;

import java.util.Iterator;
import java.util.List;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.Rule;
import cpr.context.Context;
import cpr.context.ruleacceptor.SearchRuleAcceptor;
import cpr.data.HLVector;
import cpr.helper.ScoreHelper;

public class CmdRuleSearch extends DefaultCommandHandler {
	public CmdRuleSearch() {
		super("search-rule","search for a rule: search-rule RULE_STR [SCORE]",null,null);
	}
	
	private String fixup(String s) {
		if (s.equals("-")) return " ";
		return s;
	}
	public void execute(String[] params) {
		String ruleStr1 = fixup(getParameter(params, 1));
		String ruleStr2 = fixup(getParameter(params, 2));
		String ruleStr3 = fixup(getParameter(params, 3));
		String ruleStr4 = fixup(getParameter(params, 4));
		String ruleStr5 = fixup(getParameter(params, 5));
		String ruleStr = ruleStr1 + " " + ruleStr2 + " " + ruleStr3 + " " + ruleStr4 + " " + ruleStr5;
		
		ScoreHelper helper = getCommandContext().getContext().getScoreHelper();
		int score = helper.convertStringToScore(getParameter(params, 6,"7880704"));
		HLVector aVector = new HLVector(score);
		Context ctx = getCommandContext().getContext();
		SearchRuleAcceptor ruleAcceptor= new SearchRuleAcceptor(getContext(), ruleStr, aVector);
		String path = getCommandContext().getContext().getDigitalDataFilePath();

        List hl_rules_strings_digs = null;
        hl_rules_strings_digs = getCommandContext().getContext().getIOContext().readRulesFromHLFile(path, 1,false, ruleAcceptor);
      	
      	for (Iterator iter = hl_rules_strings_digs.iterator(); iter.hasNext();) {
			Rule aRule = (Rule) iter.next();
	        Printer.println(""+aRule.ruleNrGlobal);
		}
        Printer.println("Found: "+hl_rules_strings_digs.size());
	}
    

}
